package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;

public class FastFDSClient {

    /****
     * 初始化tracker信息
     */
    static {
        try {
            //获取tracker配置文件的位置
            String path = new ClassPathResource("fdfs_client.conf").getPath();
            //加载tracker配置信息
            ClientGlobal.init(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /****
     * 文件上传
     * @param file
     * @return
     */
    public static String[] upload(FastDFSFile file) {
        String[] uploadResult = null;
        try {
            //将文件作者当做额外数据保存
            NameValuePair[] meta_list = new NameValuePair[1];
            meta_list[0] = new NameValuePair("author", file.getAuthor());

            StorageClient storageClient = getStorageClient();
            /***
             * 通过StorageClient操作Storage[文件上传],并获取返回的文件上传信息
             * 1:要上传的文件的字节数组
             * 2:文件的扩展名
             * 3:附加额外数据
             *
             * 返回数据：
             *      1:当前文件所存储的Storage的组  例如:group1
             *      2:文件存储的路径  例如: M00/02/44/2345678.jpg
             */
            uploadResult = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadResult;
    }

    /****
     * 获取文件的信息
     * @param groupName:组名
     * @param remoteFileName:文件存储的名字 M00/00/00/wKjOgl08Nw-Ab-k6AAFrWFoUuVY380.jpg
     * @return FileInfo
     */
    public static FileInfo getFile(String groupName, String remoteFileName) {
        FileInfo fileInfo = null;
        try {
            //获取storage
            StorageClient storageClient = getStorageClient();
            fileInfo = storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileInfo;
    }

    /****
     * 文件下载
     * @param groupName:组名
     * @param remoteFileName:文件存储的名字
     * @return InputStream
     */
    public static InputStream downloadFile(String groupName, String remoteFileName) {
        ByteArrayInputStream inputStream = null;
        try {
            //获取storage
            StorageClient storageClient = getStorageClient();
            byte[] bytes = storageClient.download_file(groupName, remoteFileName);
            inputStream = new ByteArrayInputStream(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return inputStream;
    }

    /****
     * 删除文件
     * @param groupName:组名
     * @param remoteFileName:文件存储的名字
     * @return InputStream
     */
    public static void deleteFile(String groupName, String remoteFileName) {
        try {
            //获取storage
            StorageClient storageClient = getStorageClient();
            storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 获取Storage信息
     * @param groupName ：Storage的组名
     */
    public static StorageServer getStorage(String groupName) {
        StorageServer storageServer = null;
        try {
            //获取tracker
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            //根据组名获取StorageServer
            storageServer = trackerClient.getStoreStorage(trackerServer, groupName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return storageServer;
    }

    /***
     * 获取Storage的IP和端口信息
     * @param groupName ：Storage的组名
     * @param remoteFileName:文件存储的名字
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) {
        ServerInfo[] storages = null;
        try {
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            //获取Storage信息
            storages = trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return storages;
    }

    /***
     * TrackerServer的Http请求地址   http://192.168.206.130:8080
     */
    public static String getTrackerUrl() {
        InetSocketAddress address = null;
        try {
            TrackerServer trackerServer = getTrackerServer();
            address = trackerServer.getInetSocketAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "http:/" + address.getHostString() + ":" + ClientGlobal.getG_tracker_http_port();
    }

    /****
     * 获取TrackerServer对象
     * @return
     * @throws IOException
     */
    public static TrackerServer getTrackerServer() throws IOException {
        //创建tracker客户端对象
        TrackerClient trackerClient = new TrackerClient();
        //获取TrackerServer信息
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerServer;
    }

    /****
     * 获取StorageClient对象
     * @return
     * @throws IOException
     */
    public static StorageClient getStorageClient() throws IOException {
        //创建tracker客户端对象
        TrackerClient trackerClient = new TrackerClient();
        //获取TrackerServer信息
        TrackerServer trackerServer = trackerClient.getConnection();
        //获取StorageClient对象
        StorageClient storageClient = new StorageClient(trackerServer, null);
        return storageClient;
    }

    public static void main(String[] args) {
        //StorageServer storageServer = getStorage("group1");
        //System.out.println(storageServer.getInetSocketAddress().getHostString());
        //System.out.println(storageServer.getInetSocketAddress().getPort());
        //System.out.println(storageServer.getStorePathIndex());

        System.out.println(getTrackerUrl());
    }
}
